/**
 * Created by dukes on 2016/3/15.
 */
class GuildShopPetItem extends eui.Component{
    constructor(){
        super();
        this.skinName = "skins.scene.elements.GuildShopPetItemSkin";
    }
    public curData;
    public setData(data){
        this.curData = data;
        this.updateDisplay();
    }
    public updateDisplay(){
        var goods = this.curData.goods;
        var index = 2;
        var goodInfo:GoodInfo;
        for(var key in goods){
            goodInfo = Model.GetGoodInfoById(parseInt(key));
            this["img_icon_"+index].source =  GameConst.Res_Root + "resource/assets/items/" + goodInfo.id + ".png";
            // this["lab_item_"+index].text = goodInfo.name+"\r"+
            //     GuildMgr.getInstance().getGoodsCnt(goodInfo.id,1)+"/"+goods[key];
            index--;
        }
        this.img_add_0.visible = this.img_bg_0.visible = this.img_icon_0.visible = this.lab_item_0.visible = index == -1;

        goodInfo = Model.GetGoodInfoById(this.curData.chipId);
        this.img_icon_3.source =  GameConst.Res_Root + "resource/assets/items/" + this.curData.chipId + ".png";
        this.lab_item_3.text =goodInfo.name;
        this.lab_count.text = "x"+this.curData.count;
        this.img_exchange.source = this.curData.exchanged ? RES.getRes("img_fuck_guild_57") : "img_fuck_guild_22";
    }
    private onExchange(evt:egret.TouchEvent){
        if( !this.curData.exchanged ){
            window["__exchangeChips"](this.curData.serialId);
        }else{
            ShowHint("已兑换该碎片，请等待刷新");
        }
    }
    private img_icon_0:eui.Image;
    private img_icon_1:eui.Image;
    private img_icon_2:eui.Image;
    private img_icon_3:eui.Image;
    private img_exchange:eui.Image;
    private lab_item_0:eui.Label;
    private lab_item_1:eui.Label;
    private lab_item_2:eui.Label;
    private lab_item_3:eui.Label;
    private lab_count:eui.BitmapLabel;

    private img_bg_0:eui.Image;
    private img_add_0:eui.Image;
    public partAdded(name:string, instance:any){
        super.partAdded(name, instance)
        if( name == "img_icon_0"){
            this.img_icon_0 = instance;
        }else if(name == "img_icon_1"){
            this.img_icon_1 = instance;
        }else if(name == "img_icon_2"){
            this.img_icon_2 = instance;
        }else if(name == "img_icon_3"){
            this.img_icon_3 = instance;
        }else if(name == "img_exchange"){
            this.img_exchange = instance;
            this.img_exchange.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onExchange, this);
        }if( name == "lab_item_0"){
            this.lab_item_0 = instance;
        }else if(name == "lab_item_1"){
            this.lab_item_1 = instance;
        }else if(name == "lab_item_2"){
            this.lab_item_2 = instance;
        }else if(name == "lab_item_3"){
            this.lab_item_3 = instance;
        }else if(name == "lab_count"){
            this.lab_count = instance;
            this.lab_count.textAlign = "right";
        }else if(name == "img_bg_0"){
            this.img_bg_0 = instance;
        }else if(name == "img_add_0"){
            this.img_add_0 = instance;
        }
    }
    private static _pool:GuildShopPetItem[] = [];
    public static gain(){
        if( GuildShopPetItem._pool.length)
            return GuildShopPetItem._pool.pop();
        return new GuildShopPetItem();
    }
    public static recycle(item:GuildShopPetItem){
        if( GuildShopPetItem._pool.indexOf(item) == -1)
            GuildShopPetItem._pool.push(item);
    }
}